package api

import (
	__ "api-gateway/basic/activity_proto"
	"api-gateway/basic/inits"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// CreateCutActivity
// @Summary      创建砍价活动
// @Description  创建砍价活动
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.CreateCutPriceActivityReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /cutPrice/createCutActivity [post]
func CreateCutActivity(c *gin.Context) {
	var req request.CreateCutPriceActivityReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.ActivityClient.CreateCutActivity(c, &__.CreateCutActivityReq{
		Title:     req.Title,
		Image:     req.Image,
		StartTime: req.StartTime,
		StopTime:  req.StopTime,
		Info:      req.Info,
		Rule:      req.Rule,
		JoinNum:   req.JoinNum,
	})
	if err != nil {
		response.Error(c, 500, "创建砍价活动失败", err.Error())
		return
	}
	response.Success(c, "创建活动成功", nil)
}

// AddCutGoods
// @Summary      添加砍价商品
// @Description  添加砍价商品
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.AddCutGoods  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /cutPrice/addCutGoods [post]
func AddCutGoods(c *gin.Context) {
	var req request.AddCutGoods
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.ActivityClient.AddCutGoods(c, &__.AddCutGoodsReq{
		GoodsId:     req.GoodsId,
		Stock:       req.Stock,
		CutMaxPrice: req.CutMaxPrice,
		CutMinPrice: req.CutMinPrice,
		CutNum:      req.CutNum,
		MinPrice:    req.MinPrice,
		MinPerson:   req.MinPerson,
		ActivityId:  req.ActivityId,
		MaxPerson:   req.MaxPerson,
		MaxBuy:      req.MaxBuy,
		MinBuy:      req.MinBuy,
	})
	if err != nil {
		response.Error(c, 500, "添加砍价商品失败", err.Error())
		return
	}
	response.Success(c, "添加砍价商品成功", nil)
}

// GetCutGoodsList
// @Summary      获取砍价商品列表
// @Description  获取砍价商品列表
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.GetCutActivityIdReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /cutPrice/getCutGoodsList [get]
func GetCutGoodsList(c *gin.Context) {
	var req request.GetCutActivityIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	list, err := inits.ActivityClient.CutGoodsList(c, &__.CutGoodsListReq{ActivityId: req.ActivityId})
	if err != nil {
		response.Error(c, 500, "查询砍价商品失败", err.Error())
		return
	}
	response.Success(c, "添加砍价商品成功", list)
}

// HelpCutPrice
// @Summary      帮忙砍价
// @Description  帮忙砍价
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.HelpCutPriceReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/helpCutPrice [post]
func HelpCutPrice(c *gin.Context) {
	var req request.HelpCutPriceReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	price, err := inits.ActivityClient.HelpCutPrice(c, &__.HelpCutPriceReq{
		UserId:         req.UserId,
		InvitationCode: req.InvitationCode,
	})
	if err != nil {
		response.Error(c, 500, "帮砍失败", err.Error())
		return
	}
	response.Success(c, "帮砍成功", price)
}

// UserJoinCutActivityReq
// @Summary      参与砍价活动
// @Description  参与砍价活动
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.UserJoinCutActivityReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/userJoinCutActivity [post]
func UserJoinCutActivityReq(c *gin.Context) {
	var req request.UserJoinCutActivityReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.ActivityClient.UserJoinCutActivity(c, &__.UserJoinCutActivityReq{
		UserId:  req.UserId,
		GoodsId: req.GoodsId,
		Num:     req.Num,
	})
	if err != nil {
		response.Error(c, 500, "参与失败", err.Error())
		return
	}
	response.Success(c, "参与成功", res)
}

// GetCutInvitationCode
// @Summary      生成邀请码
// @Description  生成邀请码
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.GetCutInvitationCode  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/getCutInvitationCode [post]
func GetCutInvitationCode(c *gin.Context) {
	var req request.GetCutInvitationCode
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.ActivityClient.GetCutInvitationCode(c, &__.GetCutInvitationCodeReq{
		UserId:  req.UserId,
		GoodsId: req.GoodsId,
		JoinId:  req.JoinId,
	})
	if err != nil {
		response.Error(c, 500, "生成失败", err.Error())
		return
	}
	response.Success(c, "生成成功", res)
}

// CutGoodsDetail
// @Summary      砍价商品详情
// @Description  砍价商品详情
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.GetCutGoodsId  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/cutGoodsDetail [get]
func CutGoodsDetail(c *gin.Context) {
	var req request.GetCutGoodsId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.ActivityClient.CutGoodsDetail(c, &__.CutGoodsDetailReq{Id: req.Id})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

// GetCutActivityList
// @Summary      砍价活动列表
// @Description  砍价活动列表
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.CutActivityStatusReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /cutPrice/getCutActivityList [get]
func GetCutActivityList(c *gin.Context) {
	var req request.CutActivityStatusReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	list, err := inits.ActivityClient.GetCutActivityList(c, &__.GetCutActivityListReq{Status: req.Status})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", list)
}

// GetCutActivityDetail
// @Summary      砍价活动详情
// @Description  砍价活动详情
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.GetCutActivityIdReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/getCutActivityDetail [get]
func GetCutActivityDetail(c *gin.Context) {
	var req request.GetCutActivityIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.ActivityClient.GetCutActivityDetail(c, &__.GetCutActivityDetailReq{ActivityId: req.ActivityId})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}

	response.Success(c, "查询成功", res)
}

// GetUserToHelpList
// @Summary      用户帮助砍价列表
// @Description  用户帮助砍价列表
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.GetUserId  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/getUserToHelpList [get]
func GetUserToHelpList(c *gin.Context) {
	var req request.GetUserId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	list, err := inits.ActivityClient.GetUserToHelpList(c, &__.GetUserToHelpListReq{UId: req.UserId})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", list)
}

// GetUserHelpList
// @Summary      用户被帮助表列表
// @Description  用户被帮助表列表
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.GetUserId  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/getUserHelpList [get]
func GetUserHelpList(c *gin.Context) {
	var req request.GetUserId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	list, err := inits.ActivityClient.GetUserHelpList(c, &__.GetUserHelpListReq{ToHelpId: req.UserId})

	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", list)
}

// GetUserJoinActivityList
// @Summary      用户参加的砍价活动记录列表
// @Description  用户参加的砍价活动记录列表
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.GetUserId  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/getUserJoinActivityList [get]
func GetUserJoinActivityList(c *gin.Context) {
	var req request.GetUserId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	list, err := inits.ActivityClient.GetUserJoinActivityList(c, &__.GetUserJoinActivityListReq{Uid: req.UserId})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", list)
}

// UpdateCutActivity
// @Summary      修改砍价活动
// @Description  修改砍价活动
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.UpdateCutActivityReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/updateCutActivity [post]
func UpdateCutActivity(c *gin.Context) {
	var req request.UpdateCutActivityReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.ActivityClient.UpdateCutActivity(c, &__.UpdateCutActivityReq{
		Id:        int64(req.Id),
		Title:     req.Title,
		Image:     req.Image,
		StartTime: req.StartTime,
		StopTime:  req.StopTime,
		Info:      req.Info,
		Rule:      req.Rule,
		Status:    int64(req.Status),
	})
	if err != nil {
		response.Error(c, 500, "修改失败", err.Error())
		return
	}
	response.Success(c, "修改成功", nil)
}

// UpdateCutGoods
// @Summary      修改砍价商品
// @Description  修改砍价商品
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.UpdateCutGoodsReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/updateCutGoods [post]
func UpdateCutGoods(c *gin.Context) {
	var req request.UpdateCutGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.ActivityClient.UpdateCutGoods(c, &__.UpdateCutGoodsReq{
		Id:          int64(req.Id),
		ActivityId:  int64(req.ActivityId),
		GoodsId:     int64(req.GoodsId),
		GoodsName:   req.GoodsName,
		GoodsImg:    req.GoodsImg,
		Stock:       int64(req.Stock),
		Desc:        req.Desc,
		BasePrice:   float32(req.BasePrice),
		CutMaxPrice: float32(req.CutMaxPrice),
		CutMinPrice: float32(req.CutMinPrice),
		CutNum:      req.CutNum,
		IsMyCut:     int64(req.IsMyCut),
		MinPrice:    float32(req.MinPrice),
		MaxPerson:   req.MaxPerson,
		MinPerson:   req.MinPerson,
		MaxBuy:      req.MaxBuy,
		MinBuy:      req.MinBuy,
	})
	if err != nil {
		response.Error(c, 500, "修改失败", err.Error())
		return
	}
	response.Success(c, "修改成功", nil)
}

// DetailCutActivity
// @Summary      砍价活动详情
// @Description  砍价活动详情
// @Tags         砍价模块
// @Accept      json
// @Produce     json
// @Param        address  query   request.GetCutActivityIdReq  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /cutPrice/detailCutActivity [get]
func DetailCutActivity(c *gin.Context) {
	var req request.GetCutActivityIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	res, err := inits.ActivityClient.GetCutActivityDetail(c, &__.GetCutActivityDetailReq{ActivityId: req.ActivityId})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

// 参与记录列表
func ListJoin(c *gin.Context) {

	res, err := inits.ActivityClient.CutJoinList(c, &__.CutJoinListReq{})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

// 用户帮助砍价列表
func ListHelp(c *gin.Context) {
	res, err := inits.ActivityClient.CutHelpList(c, &__.CutHelpListReq{})
	if err != nil {
		response.Error(c, 500, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

// 删除参与记录
func DelCutJoin(c *gin.Context) {
	var req request.GetCutActivityIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.ActivityClient.JoinDel(c, &__.JoinDelReq{Id: req.ActivityId})
	if err != nil {
		response.Error(c, 500, "删除失败", err.Error())
		return
	}
	response.Success(c, "删除成功", nil)
}

// 删除砍价商品
func DelCutGoods(c *gin.Context) {
	var req request.GetCutActivityIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.ActivityClient.CutGoodsDel(c, &__.CutGoodsDelReq{Id: req.ActivityId})
	if err != nil {
		response.Error(c, 500, "删除失败", err.Error())
		return
	}
	response.Success(c, "删除成功", nil)
}

// 删除砍价活动
func DelCutActivity(c *gin.Context) {
	var req request.GetCutActivityIdReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.ActivityClient.CutActivityDel(c, &__.CutActivityDelReq{Id: req.ActivityId})
	if err != nil {
		response.Error(c, 500, "删除失败", err.Error())
		return
	}
	response.Success(c, "删除成功", nil)
}
